/*
  格式化树形
  datas需要过滤的数据
  最外层节点的pid
  defaultProp为树形数据的默认配置
  */
const getListData = function getListData(datas, bigPid, defaultProp) {
	if (!defaultProp) {
		defaultProp = {
			label: 'label',
			id: 'id',
			pid: 'pid',
		};
	}
	let dataArray = [];
	datas.forEach(function(data) {
		let pid = data[defaultProp.pid];
		if (pid == bigPid) {
			let objTemp = {};
			for (var pl in data) {
				//数组对象遍历
				objTemp[pl] = data[pl];
			}
			dataArray.push(objTemp);
		}
	});
	return data2treeDG(datas, dataArray, defaultProp);
};

function data2treeDG(datas, dataArray, defaultProp) {
	for (let j = 0; j < dataArray.length; j++) {
		let dataArrayIndex = dataArray[j];
		let childrenArray = [];
		let Id = dataArrayIndex[defaultProp.id];
		for (let i = 0; i < datas.length; i++) {
			let data = datas[i];
			let pid = data[defaultProp.pid];
			if (pid == Id) {
				//判断是否为儿子节点
				let objTemp = {};
				for (var pl in data) {
					//数组对象遍历
					objTemp[pl] = data[pl];
				}
				childrenArray.push(objTemp);
			}
		}
		dataArrayIndex.children = childrenArray;
		if (childrenArray.length > 0) {
			//有儿子节点则递归
			data2treeDG(datas, childrenArray, defaultProp);
		}
	}
	return dataArray;
}
export default getListData;
